package com.ps.camera;

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

import org.apache.log4j.Level;

import android.os.Environment;
import de.mindpipe.android.logging.log4j.LogConfigurator;

public class SLogger {
	public static void loadLogConfigurator(File storagePath)
    {
    	LogConfigurator logConfigurator = new LogConfigurator();
    	logConfigurator.setFileName(storagePath
				+ File.separator + "CameraApp" + File.separator + "logs"
				+ File.separator + "log4j.txt");
		logConfigurator.setRootLevel(Level.DEBUG);
		logConfigurator.setLevel("org.apache", Level.ERROR);
		logConfigurator.setFilePattern("%d %-5p [%c{2}]-[%L] %m%n");
		logConfigurator.setMaxFileSize(1024 * 1024 * 5);
		logConfigurator.setImmediateFlush(true);
		logConfigurator.configure();
    }
	
	public static File getStoragePath()
    {
    	boolean mExternalStorageAvailable = false;
        boolean mExternalStorageWritable = false;
        
        String state = Environment.getExternalStorageState();
        
        try
        {
        	File storagePath = null;
        	if (Environment.MEDIA_MOUNTED.equals(state))
            {
        		// We can read and write the media
            	mExternalStorageAvailable = mExternalStorageWritable = true;
            }
            else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
            {
            	// We can only read the media
            	mExternalStorageAvailable = true;
            	mExternalStorageWritable = false;
            }
            else
            {
            	// Something else is wrong. It may be one of many other states, but all we need
            	// to know is we can neither read nor write
            	mExternalStorageAvailable = mExternalStorageWritable = false;
            }
            
            if (mExternalStorageAvailable && mExternalStorageWritable)
            {
            	storagePath = Environment.getExternalStorageDirectory();
            }
            else
            {
            	storagePath = Environment.getDataDirectory();
            }
            return storagePath;
        }
        catch(Exception e)
        {
        	//log.error(e.getMessage());
        }
        return null;
    }
	
	public static String getStackTrace()
	{
		String stack = null;
		StackTraceElement[] elements = new Throwable().getStackTrace();
		for (int i = 0; i < elements.length; i++)
		{
			stack += elements[i] + "\r\n";
		}
		
		return stack;
	}
}
